package com.example.coolweather.util;

import android.text.TextUtils;

import com.example.coolweather.gson.Weather;
import com.example.coolweather.info.CityInfo;
import com.example.coolweather.info.CountyInfo;
import com.example.coolweather.info.ProvinceInfo;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Utility {

    // 解析和处理服务器返回的省级数据
    public static List<ProvinceInfo> handleProvinceResponse(String respose) {
        List<ProvinceInfo> list = new ArrayList<>();
        if (!TextUtils.isEmpty(respose)) {
            try {
                JSONArray allProvince = new JSONArray(respose);
                for (int i = 0; i < allProvince.length(); i++) {
                    JSONObject provinceObject = allProvince.getJSONObject(i);
                    ProvinceInfo info = new ProvinceInfo();
                    info.provinceName = provinceObject.getString("name");
                    info.provinceCode = provinceObject.getInt("id");
                    list.add(info);
                }
                return list;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    // 解析和处理服务器返回的市级数据
    public static List<CityInfo> handleCityResponse(String respose, int provinceId) {
        List<CityInfo> list = new ArrayList<>();
        if (!TextUtils.isEmpty(respose)) {
            try {
                JSONArray allCities = new JSONArray(respose);
                for (int i = 0; i < allCities.length(); i++) {
                    JSONObject cityObject = allCities.getJSONObject(i);
                    CityInfo info = new CityInfo();
                    info.cityName = cityObject.getString("name");
                    info.cityCode = cityObject.getInt("id");
                    info.provinceId = provinceId;
                    list.add(info);
                }
                return list;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    // 解析和处理服务器返回的县级数据
    public static List<CountyInfo> handleCountyResponse(String respose, int cityId) {
        List<CountyInfo> list = new ArrayList<>();
        if (!TextUtils.isEmpty(respose)) {
            try {
                JSONArray allCounties = new JSONArray(respose);
                for (int i = 0; i < allCounties.length(); i++) {
                    JSONObject countybject = allCounties.getJSONObject(i);
                    CountyInfo info = new CountyInfo();
                    info.countyName = countybject.getString("name");
                    info.weatherId = countybject.getString("weather_id");
                    info.cityId = cityId;
                    list.add(info);
                }
                return list;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    // 将返回的JSON数据解析成Weather实体类
    public static Weather handleWeatherResponse(String response) {
        try {
            JSONObject jsobj = new JSONObject(response);
            // "HeWeather"是和风天气API返回数据的固定字段名
            JSONArray jsArray = jsobj.getJSONArray("HeWeather"); // 从JSONObject中获取名为"HeWeather"的JSON数组
            String weatherCont = jsArray.getJSONObject(0).toString(); // 获取数组中第一个元素(索引0)的JSON对象
            return new Gson().fromJson(weatherCont, Weather.class); // 使用Google的Gson库将JSON字符串weatherCont转换为Java对象
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
